package com.ybkj.o2o.mc.service.impl;

import com.ybkj.o2o.mc.common.query.EnterpriseQuery;
import com.ybkj.o2o.mc.entity.Enterprise;
import com.ybkj.o2o.mc.mapper.EnterpriseMapper;
import com.ybkj.o2o.mc.service.EnterpriseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by shine on 15/8/19.
 */
@Service
public class DefaultEnterpriseService implements EnterpriseService {

  @Autowired
  private EnterpriseMapper enterpriseMapper;


  @Override
  public boolean exists(String name, String appKey, Long companyId) {

    Enterprise enterprise = enterpriseMapper.findByNameAndAppkey(name, appKey, companyId);

    return null != enterprise;
  }


  @Override
  public int insert(Enterprise enterprise) {

    if (null == enterprise.getCreated()) {
      enterprise.setCreated(new Date());
    }
    if (null == enterprise.getUpdated()) {
      enterprise.setUpdated(new Date());
    }

    enterprise.setVersion(1);

    return enterpriseMapper.insert(enterprise);
  }


  @Override
  public Enterprise findByIdAndAppKey(Long enterpriseId, String appKey) {

    return enterpriseMapper.selectByIdAndAppKey(enterpriseId, appKey);
  }

  @Override
  public int updateEnterprise(Enterprise enterprise) {

    return enterpriseMapper.updateByPrimaryKey(enterprise);
  }

  @Override
  public int delete(Long enterpriseId, String appKey) {

    return enterpriseMapper.deleteByPrimaryKey(enterpriseId, appKey);
  }

  @Override
  public int updateBalance(Enterprise enterprise) {
    return enterpriseMapper.updateBalance(enterprise);
  }

  @Override
  public List<Enterprise> findByEnterpriseQuery(EnterpriseQuery enterpriseQuery) {

    return enterpriseMapper.selectByEnterpriseQuery(enterpriseQuery);
  }

  @Override
  public Long countByEnterpriseQuery(EnterpriseQuery enterpriseQuery) {

    return enterpriseMapper.countByEnterpriseQuery(enterpriseQuery);

  }

  @Override
  public Enterprise fetch(Long id) {

    return enterpriseMapper.selectByPrimaryKey(id);
  }


  @Override
  public int updateCompanyName(Long companyId, String abbreviation, String appKey) {

    return enterpriseMapper.updateCompanyName(companyId, abbreviation, appKey);
  }

  @Override
  public List<Enterprise> findByGradeId(Long gradeId, String appKey) {

    return enterpriseMapper.findByGradeId(gradeId,appKey);
  }

  @Override
  public int batchUpdateGrade(Long gradeId, String appKey) {

    return enterpriseMapper.batchUpdateGrade(gradeId,appKey);
  }

  @Override
  public int disposeGrade(Long gradeId, String gradeName, Long companyId, String appKey) {

    return enterpriseMapper.disposeGrade(gradeId,gradeName,companyId,appKey);
  }


}
